<template>
  <div>
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column prop="id" label="编号" width="180" />
      <el-table-column prop="type" label="类型" width="180" />
      <el-table-column prop="scale" label="抽成比例">
        <template #default="scope">
          {{ scope.row.scale + '%' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button
              size="small"
              type="primary"
              @click="handleEdit(scope.row)"
          >
            编辑
          </el-button>
          <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.row)"
              style="margin-left: 8px"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑对话框 -->
    <el-dialog
        v-model="dialogVisible"
        title="编辑抽成比例"
        :width="'30%'"
    >
      <el-form
          :model="formData"
          label-width="100px"
          :rules="rules"
          ref="formRef"
      >
        <el-form-item label="类型" prop="type">
          <el-input
              v-model="formData.type"
              placeholder="请输入类型"
              :disabled="true"
          />
        </el-form-item>
        <el-form-item label="抽成比例(%)" prop="scale">
          <el-input
              v-model.number="formData.scale"
              placeholder="请输入抽成比例"
              type="number"
              min="0"
              max="100"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref, reactive, toRefs } from "vue";
import { ElMessage } from 'element-plus';
import server from "../utils/request.js";

// 定义数据类型接口
interface TableItem {
  id: number;
  type: string;
  scale: number;
}

const tableData = ref<TableItem[]>([]);
const dialogVisible = ref(false);
const formRef = ref();

// 表单数据
const formData = reactive({
  id: 0,
  type: '',
  scale: 0
});

// 表单验证规则
const rules = {
  type: [
    { required: true, message: '请输入类型', trigger: 'blur' }
  ],
  scale: [
    { required: true, message: '请输入抽成比例', trigger: 'blur' },
    { type: 'number', min: 0, max: 100, message: '抽成比例必须在0-100之间', trigger: 'blur' }
  ]
};

onMounted(() => {
  loadData();
});

// 加载数据
async function loadData() {
  try {
    const { data } = await server.get("system");
    if (data.code === 200) {
      tableData.value = data.data;
    }
  } catch (error) {
    console.error("加载数据失败:", error);
    ElMessage.error('加载数据失败');
  }
}

// 打开编辑对话框
const handleEdit = (row: TableItem) => {
  // 复制行数据到表单
  formData.id = row.id;
  formData.type = row.type;
  formData.scale = row.scale;
  dialogVisible.value = true;
};

// 提交编辑表单
const handleSubmit = async () => {
  // 表单验证
  if (!formRef.value) return;
  const valid = await formRef.value.validate();
  if (!valid) return;

  try {
    // 调用后端更新接口
    const { data } = await server.put(`system`, formData);
    if (data.code === 200) {
      ElMessage.success('修改成功');
      // 更新表格数据
      const index = tableData.value.findIndex(item => item.id === formData.id);
      if (index !== -1) {
        tableData.value[index] = { ...formData };
      }
      dialogVisible.value = false;
    }
  } catch (error) {
    console.error("更新数据失败:", error);
    ElMessage.error('修改失败');
  }
};

// 删除操作
const handleDelete = async (row: TableItem) => {
  try {
    // 实际项目中应该先显示确认对话框
    const { data } = await server.delete(`system/${row.id}`);
    if (data.code === 200) {
      ElMessage.success('删除成功');
      // 从表格中移除数据
      tableData.value = tableData.value.filter(item => item.id !== row.id);
    }
  } catch (error) {
    console.error("删除数据失败:", error);
    ElMessage.error('删除失败');
  }
};
</script>